package com.lemon.mybatisplus.handler;

import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.lemon.mybatisplus.properties.MybatisPlusConfigProperties;
import com.lemon.security.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.schema.Column;

import java.util.List;

/**
 * @author 李猛
 * @datetime 2024/2/19 16:57
 * @description 多租户配置
 */
@Slf4j
public class MultiTenantHandler implements TenantLineHandler {
    private final MybatisPlusConfigProperties properties;

    public MultiTenantHandler(MybatisPlusConfigProperties properties) {
        this.properties = properties;
    }

    @Override
    public Expression getTenantId() {
        return new LongValue(JwtUtil.getCurrentUser().getTenantId());
    }

    @Override
    public String getTenantIdColumn() {
        return properties.getTenantColumnId();
    }

    @Override
    public boolean ignoreTable(String tableName) {
        return properties.getTenantIgnoreTable().contains(tableName);
    }

    @Override
    public boolean ignoreInsert(List<Column> columns, String tenantIdColumn) {
        return TenantLineHandler.super.ignoreInsert(columns, tenantIdColumn);
    }
}
